import framDate from '@/common/framDate.js'
export default {
	baseUrl: "https://api.jingshenyu.com/api/",
	// baseUrl: "https://cs.pdsyun.com",
	public:{
		userLogin: 'user/login', //公众号登录
		xcx_login: 'user/xcx_login', //微信登录
		zfb_login: 'user/zfb_login', //支付宝登录
		userBind: "user/bind",//绑定接口
		index: 'index/index', //首页
		search: "house/search", //查询
		order_list: "order/list", //缴费记录
		order_info: "order/info", //缴费详情
		order_add: "order/add", //拉起支付1微信 2支付宝
		house_community:"house/community", //选择小区
	},
	common:{
		informList: "inform/list", //消息通知列表
		informDetail: 'inform/details', //消息通知详情
	},
	async uniRequ(url,data={},method="post") {
		if(!uni.getStorageSync('token') && url.indexOf('login') == -1){
			this._login(url,data,method);
			return
		}
		url = this.baseUrl + url;
		let [error, res] = await uni.request({
			url: url,
			method: method,
			data: data,
			header: {
				token: uni.getStorageSync('token'),
				"content-type": 'application/json'
			},
			timeout: 3500
		})
		if(res.statusCode == 200){
			if(res.data.code == 1){
				return {code: 1,data:res.data.data}
			}else{
				uni.showToast({
					title: res.data.msg,
					icon:'none'
				});
				if(res.data.code == 401){
					this._login();
					return {code: 0}
				}else{
					return {code: 0}
				}
			}
		}else {
			this._login();
			return {code: 0}
		}
	},
	_login(url,data,method){
		// #ifdef MP-WEIXIN
		let that = this;
		wx.login({
			async success(res) {
				let yyds = await that.uniRequ(that.public.xcx_login,{code: res.code},'get');
				console.log(yyds)
				if(yyds.code == 1){
					uni.setStorageSync('token',yyds.data.token);
					if(url){
						that.uniRequ(url,data,method)
					}
				}
			}
		})
		// #endif
		// #ifdef MP-ALIPAY
		let that = this;
		my.getAuthCode({
			scopes:['auth_base'],
			async success(res) {
				let yyds = await that.uniRequ(that.public.zfb_login,{code: res.authCode},'get');
				console.log(yyds)
				if(yyds.code == 1){
					uni.setStorageSync('token',yyds.data.token);
					if(url){
						that.uniRequ(url,data,method)
					}
				}
			}
		})
		// #endif
		// #ifdef H5
		location.href = `${this.baseUrl}user/get_code`;	
		// #endif
	}
}
